package lucene.server.database.meta;

import lucene.server.database.model.ColumnModel;
import lucene.server.database.model.DatabaseModel;
import lucene.server.database.model.TableModel;
import lucene.server.database.utils.DatabaseUtil;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class SqlServerDialect extends AbstractDialect {
    public SqlServerDialect(String databaseType) {
        super(databaseType);
    }

    @Override
    public Map<String,DatabaseModel> getDataBases(Connection connection) {
        Map<String,DatabaseModel> databaseModels = new HashMap<>();
        ResultSet catalogs = null;
        try {
            catalogs = connection.getMetaData().getCatalogs();
            while (catalogs.next()) {
                DatabaseModel databaseModel = new DatabaseModel();
                databaseModel.setCatalog(catalogs.getString("TABLE_CAT"));
                databaseModel.setName(catalogs.getString("TABLE_CAT"));
                databaseModel.setSchema("dbo");
                databaseModel.setType("sqlserver");
                databaseModel.setType(databaseType);
                databaseModels.put(databaseModel.getName(),databaseModel);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DatabaseUtil.close(connection,catalogs);
        }
        return databaseModels;
    }
    @Override
    public void setColumns(Connection connection, DatabaseModel databaseModel, TableModel tableModel) {
        super.setColumns(connection,databaseModel,tableModel);
    }

}
